package wawi.util;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import wawi.actions.Artikel;
import wawi.actions.ArtikelDetails;
import wawi.actions.Bestellung;
import wawi.actions.BestellungPosition;
import wawi.db.DbConnector;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.BarcodeEAN;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfWriter;



public class TestBarcode {

	private final static float P_CM = 28.346f;
	
	private final static float W_CM = 5.25f;
	private final static float H_CM = 2.97f;
	
	private final static float WIDTH = W_CM * P_CM;
	private final static float HEIGHT = H_CM * P_CM;
	
	   /**
     * Generates a PDF file with different types of barcodes.
     * 
     * @param args
     *            no arguments needed here
     * @throws DocumentException
     * @throws IOException
     */
    public static void main(String[] args) throws IOException, DocumentException
    {
        new TestBarcode().createPdf("D:\\test.pdf", DbConnector.getInstance().readBestellung(22-3), 5);
    }
 
    /**
     * Creates a PDF document.
     * @param filename the path to the new PDF document
     * @throws    DocumentException 
     * @throws    IOException
     */
    public void createPdf(String filename, Bestellung bestellung, int versatz) throws IOException, DocumentException
    {    	        
    	List<ArtikelDetails> details = new ArrayList<ArtikelDetails>();
    	bestellung.loadPositions();
    	
    	for (int i=0; i<versatz; i++)
    		details.add(null);
    	
    	for (BestellungPosition pos : bestellung.getPositionen())
    	{
    		for (int i=0; i<pos.getMenge(); i++)
    			details.add(pos.getArtikelDetails());
    	}
    	    	    	
    	
        // step 1
        Document document = new Document();
        // step 2
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
        // step 3
        document.open();
        // step 4
        PdfContentByte cb = writer.getDirectContent();
 
        int pos = 0;
        
        do
        {
	        //Etikett
	        float x = 0;
	        float y = 9*HEIGHT;	        
	        
	        for (int i=0; i<10; i++) // 10 Zeilen
	        {
	        	x = 0;
	        	
	        	for (int j=0; j<4; j++) // 4 Spalten
	        	{
	        		if (pos < details.size())
	        		{
	        			ArtikelDetails ad = details.get(pos);        			
	        			pos++;
	        			
	        			if (ad!=null) //null ist versatz
	        				drawEtikett(cb, x, y, ad.getArtikel(), ad);
	        			
	        			x += WIDTH;
	        		}
	        	}        	
	        	y -= HEIGHT;
	        }
	        
	        if (pos<details.size())
	        	document.newPage();
        }
        while (pos < details.size());
        
        
        
        // step 5
        document.close();
    }

	public void drawEtikett(PdfContentByte cb, float x, float y, Artikel art, ArtikelDetails details) throws DocumentException, IOException
	{
		
    	BaseFont bf = BaseFont.createFont(
    			BaseFont.HELVETICA,
    			BaseFont.CP1252,
    			BaseFont.NOT_EMBEDDED);
    	
    	BaseFont bf_bold = BaseFont.createFont(
    			BaseFont.HELVETICA_BOLD,
    			BaseFont.CP1252,
    			BaseFont.NOT_EMBEDDED);
		
		float d_x = 33f;
		float d_y = 10f;
		
		// EAN 13
        BarcodeEAN codeEAN = new BarcodeEAN();       
        codeEAN.setBarHeight(25f);       
        codeEAN.setCode(details.getPrintEAN());          
        
        cb.beginText();
        cb.setFontAndSize(bf, 8);
        cb.setTextMatrix( x + d_x, y + d_y + 50f);
        cb.showText(art.getTextKurz());
        cb.endText();
        
        cb.beginText();
        
        cb.setTextMatrix( x +d_x, y + d_y + 40f);
        
        cb.setFontAndSize(bf, 5);
        cb.showText("ART ");
        
        cb.setFontAndSize(bf_bold, 6);
        cb.showText(""+art.getArtNr());
        
        cb.setFontAndSize(bf, 5);
        cb.showText("  COL ");
        
        cb.setFontAndSize(bf_bold, 6);
        cb.showText(details.getFarbe());
        
        cb.setFontAndSize(bf, 5);
        cb.showText("  SIZE ");
        
        cb.setFontAndSize(bf_bold, 6);
        cb.showText(details.getGroesse());
        
        cb.endText();
        
        
        Image img = codeEAN.createImageWithBarcode(cb, null, null);
        
        
        
        //Position + Add
        img.setAbsolutePosition(x + d_x, y + d_y);
        cb.addImage(img);

        //Rahmen
        cb.saveState();
        cb.rectangle(x + 0f, y+0f, W_CM*P_CM, H_CM*P_CM);
        cb.stroke();
        cb.restoreState();
	}
}
